// 定义拦截器
import axios from 'axios';
import {Message} from "element-ui";

axios.interceptors.response.use(resp => {
    if (resp.status == 200 && resp.data && resp.data.status == 500) {
        if (resp.data.msg) {
            Message.error({message : resp.data.msg});
            return;
        }
    }
    if (resp.data.msg) {
        Message.success({message : resp.data.msg});
    }
    return resp.data;
},error => {
    if (error.response) {
        if (error.response.status == 404 || error.response.status == 504) {
            Message.error({message: '服务器被吃了( ╯□╰ )'});
        } else if (error.response.status == 403) {
            Message.error({message: '对不起，权限不足！'});
        } else if (error.response.status == 401) {
            Message.error({message: '对不起，没有登录！'});
        } else if (error.response.status == 400) {
            Message.error({message: '对不起，参数有问题！'});
        } else if (error.response.status == 405) {
            Message.error({message: '对不起，请求方式不对！'});
        }else{
            Message.error({message: '未知错误！'});
        }
    }
    return;
})

// 封装请求 api
export const getRequest = (url,params) => {
    return axios({
        method : 'get',
        url : url,
        data : params
    })
}

export const postRequest = (url,params) => {
    return axios({
        method : 'post',
        url : url,
        data : params
    })
}

export const putRequest = (url,params) => {
    return axios({
        method : 'put',
        url : url,
        data : params
    })
}

export const delRequest = (url,params) => {
    return axios({
        method : 'delete',
        url : url,
        data : params
    })
}